package view.tank
{
	import data.Globals;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import utils.GameUtils;
	
	import view.base.BaseView;
	
	public class BaseTank extends BaseView
	{
		private var _bulletType:String;
		
		private var _shoutRadius:int;
		
		private var _shoutGap:int;
		
		private var preTime:Number;
		
		private var _rotateSpeed:Number=2;
		/**
		 * 射击对象 
		 */		
		public var shoutTarget:DisplayObject;
		
		private var monsterAry:Array = Globals.gameView.getMonsterAry();

		private var cur:Number;

		private var next:Number;
		
		public function BaseTank()
		{
			super();
			this.mouseEnabled =false;
			this.mouseChildren = false;
		}
		
		override protected function loop(event:Event):void{
			if(isCloseLoop){
				return;
			}
			if(shoutTarget){
				var distance:int = GameUtils.getTwoPointDistance(new Point(shoutTarget.x,shoutTarget.y),new Point(this.x,this.y));
				if(distance > shoutRadius){
					findTarget();
				}
			}else{
				findTarget();
			}
			if(shoutTarget){
				cur = this.rotation;
				if(Math.abs(next - cur)<180){
					if(next>cur){
						cur = this.rotation+=rotateSpeed;
					}else{
						cur = this.rotation-=rotateSpeed;
					}
				}else{
					if(cur<next){
						cur = this.rotation-rotateSpeed;
					}else{
						cur = this.rotation+rotateSpeed;
					}
				}
				turnDirection();
			
			}
		}
		
		private function turnDirection():void
		{
			next = GameUtils.getRotation(new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y));
			if(Math.abs(next - cur)<rotateSpeed*2+1){
				this.rotation = next;
				shout();
			}
		}
		
		public function shout():void{
			var isShoot:Boolean;
			if(preTime){
				var curUse:Number = getTimer() - preTime;
				if(curUse > shoutGap){
					preTime = getTimer();
					isShoot = true;
				}
			}else{
				preTime = getTimer();
				isShoot = true;
			}
			if(isShoot)Globals.gameView.shoutBullet(bulletType,new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y));
		}
		
		private function findTarget():void
		{
			var distance:int=0;
			for(var i:int=0;i<monsterAry.length;i++){
				distance = GameUtils.getTwoPointDistance(new Point(monsterAry[i].x,monsterAry[i].y),new Point(this.x,this.y))
				if(distance<shoutRadius){
					shoutTarget = monsterAry[i];
					return;
				}
			}
			shoutTarget = null;
		}
		
		/**
		 * 射击半径 
		 * @return 
		 * 
		 */
		public function get shoutRadius():int
		{
			return _shoutRadius;
		}
		
		public function set shoutRadius(value:int):void
		{
			_shoutRadius = value;
		}
		
		/**
		 * 子弹发射频率 间隔为毫秒 
		 */
		public function get shoutGap():int
		{
			return _shoutGap;
		}
		
		/**
		 * @private
		 */
		public function set shoutGap(value:int):void
		{
			_shoutGap = value;
		}

		public function get bulletType():String
		{
			return _bulletType;
		}

		public function set bulletType(value:String):void
		{
			_bulletType = value;
		}

		public function get rotateSpeed():Number
		{
			return _rotateSpeed;
		}

		public function set rotateSpeed(value:Number):void
		{
			_rotateSpeed = value;
		}


	}
}